package com.weiai.wedding.web.home;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.weiai.wedding.entity.cases.Cases;
import com.weiai.wedding.entity.cases.CasesContent;
import com.weiai.wedding.entity.cases.Resources;
import com.weiai.wedding.entity.user.User;
import com.weiai.wedding.service.ShiroDbRealm.ShiroUser;
import com.weiai.wedding.service.cases.CasesService;
import com.weiai.wedding.service.user.UserService;
import com.weiai.wedding.util.ConUtils;
import com.weiai.wedding.util.DataTablesParamUtility;
import com.weiai.wedding.util.DataTablesReturnObject;
import com.weiai.wedding.util.MD5;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import sun.misc.BASE64Decoder;

/**
 * LoginController负责打开登录页面(GET请求)和登录出错页面(POST请求)，

 * 真正登录的POST请求由Filter完成,
 * 
 * @author wangwieheng
 */
@Controller
@RequestMapping(value = "/h")
public class HomeController {
	@Autowired
	private UserService userService;
	@Autowired
	private CasesService casesService;
	@RequestMapping(value = "")
	public String list(Model model){
		List<Cases> casesList=casesService.findCasesList();
		List<Cases> casesL=new ArrayList<Cases>();
		if(casesList.size()>6){
			for(int i=0;i<6;i++){
				casesL.add(casesList.get(i));
			}
		}else{
			casesL=casesList;
		}
		model.addAttribute("casesList", casesL);
		return "home/home";
	}
	@RequestMapping(value = "viewPhoto/{id}")
	public void getPhotoById (@PathVariable("id")Long id,HttpServletResponse response){   
	    Cases cases = casesService.findCasesById(id);
	    String path = cases.getResourcesDir();
	    response.setContentType("image/jpeg");   
	    response.setCharacterEncoding("UTF-8");   
	    OutputStream outputSream;
	    FileInputStream is;
		try {
			outputSream = response.getOutputStream();
			is = new FileInputStream (path);
			int len = 0;   
		    byte[] buf = new byte[1024];   
		    while ((len = is.read(buf, 0, 1024)) != -1) {   
		        outputSream.write(buf, 0, len);   
		    }   
		    outputSream.close();
		    is.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}  
	
	@RequestMapping(value="view/{id}")
	public String viewForm(@PathVariable("id")   
		    Long id,Model model){
		Cases cases = casesService.findCasesById(id);
		model.addAttribute("cases", cases);
		return "home/casesDetail";
	}
//	@RequestMapping(value="viewImg/{id}")
//	public void viewImg(@PathVariable("id")Long id,HttpServletResponse response){
//		CasesContent content = casesService.findCasesContentById(id);
//		response.setContentType("image/jpeg");   
//	    response.setCharacterEncoding("UTF-8");   
//	    OutputStream outputSream;
//		BASE64Decoder base = new BASE64Decoder();
//		if(content!=null){
//			try {
//				outputSream = response.getOutputStream();
//				byte[] decodeBuffer = base.decodeBuffer(new String(content.getResContent(), "UTF-8"));
//				outputSream.write(decodeBuffer);
//				outputSream.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
	@RequestMapping(value="contactUs")
	public String contactUs(Model model){
		return "home/contactUs";
	}
	@RequestMapping(value="aboutUs")
	public String aboutUs(Model model){
		return "home/aboutUs";
	}
	@RequestMapping(value = "/showCases")
	public String showCaseslist(Model model,HttpServletRequest request){
		Sort sort = new Sort(Direction.DESC, "casesId");
		//ageable pageable = DataTablesParamUtility.buildPage(req,sort);
		//int pagesize = Integer.parseInt(request.getParameter("iDisplayLength"));
		//int pageno = Integer.parseInt(request.getParameter("iDisplayStart"))/pagesize;
		int pagesize=6;
		int pageno=0;//第几页
		String pagenoStr=request.getParameter("pageno");
		if(StringUtils.isNotBlank(pagenoStr)){
			pageno=Integer.valueOf(pagenoStr);
			pageno=pageno-1;
		}
		Pageable pager = new PageRequest(pageno,pagesize,sort);
		Page<Cases> page = casesService.getAll(pager);
		model.addAttribute("page",page);
		model.addAttribute("pageno", pageno);
		return "home/showCases";
	}
}
